import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { StudentsApi } from '#/api/base/students';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

import { z } from '#/adapter/form';
import DictTag from '#/components/dict-tag/dict-tag.vue';
/** 新增/修改的表单 */
export function baseFormSchema(): VbenFormSchema[] {
  return [
    // 基础信息
    {
      fieldName: 'studentsId',
      component: 'Input',
      group: 'base',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'studentsCode',
      label: '学号',
      group: 'base',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入学号' },
      rules: 'required',
    },
    {
      fieldName: 'studentName',
      label: '学生姓名',
      group: 'base',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入学生姓名' },
      rules: 'required',
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      group: 'base',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE, 'string'),
        rules: 'required',
        placeholder: '请选择证件类型',
      },
      rules: 'required',
    },
    {
      fieldName: 'idCard',
      label: '证件号码',
      group: 'base',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入证件号码' },
      rules: 'required',
    },
    {
      fieldName: 'cellphone',
      label: '手机',
      group: 'base',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入手机',
      },
      rules: 'mobile',
    },
  ];
}

export function schoolFormSchema(): VbenFormSchema[] {
  return [
    // 学校信息
    {
      fieldName: 'enterYear',
      label: '入学年份',
      group: 'school',
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'period',
      label: '学段',
      group: 'school',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_PERIOD, 'string'),
        placeholder: '请选择学段',
      },
    },
    {
      fieldName: 'schoolId',
      label: '学校',
      group: 'school',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        options: [],
        placeholder: '请选择学校',
      },
    },
    {
      fieldName: 'gradeId',
      label: '年级',
      group: 'school',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        options: [],
        placeholder: '请选择年级',
      },
    },
    {
      fieldName: 'classId',
      label: '班级',
      group: 'school',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        options: [],
        placeholder: '请选择班级',
      },
    },
  ];
}

export function otherFormSchema(): VbenFormSchema[] {
  return [
    // 其他信息
    {
      fieldName: 'photoUrl',
      label: '学生照片',
      group: 'other',
      component: 'ImageUpload',
      componentProps: {
        params: {
          fileCategoriesCode: 'RL',
          objectId: 'XSZP',
          objectType: '学生照片',
        },
      },
    },
    {
      fieldName: 'email',
      label: '电子邮箱',
      group: 'other',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入电子邮箱' },
    },
    {
      fieldName: 'weixin',
      label: '微信',
      group: 'other',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入微信' },
    },
    {
      fieldName: 'healthStatus',
      label: '健康状况',
      group: 'other',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_HEALTH_CONDITIONS, 'string'),
        placeholder: '请选择健康状况',
      },
    },
    {
      fieldName: 'nation',
      label: '民族',
      group: 'other',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_NATION, 'string'),
        placeholder: '请选择民族',
      },
    },
    {
      fieldName: 'faith',
      label: '宗教信仰',
      group: 'other',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_FAITH, 'string'),
        placeholder: '请选择宗教信仰',
      },
    },
    {
      fieldName: 'residenceType',
      label: '户口性质',
      group: 'other',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_RESIDENCETYPE, 'number'),
        placeholder: '请选择户口性质',
      },
    },
    {
      fieldName: 'politicalInfo',
      label: '政治面貌',
      group: 'other',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_POLITICALINFO, 'string'),
        placeholder: '请选择政治面貌',
      },
    },
    {
      fieldName: 'studentType',
      label: '学生类别',
      group: 'other',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.EDU_STUDENT_TYPE, 'string'),
        placeholder: '请选择学生类别',
      },
    },
    {
      fieldName: 'onlyChild',
      label: '独生子女',
      group: 'other',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'isOrphan',
      label: '孤儿',
      group: 'other',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'isSpecialcare',
      label: '烈士或优抚子女',
      group: 'other',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'isLeftbehind',
      label: '留守儿童',
      group: 'other',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'isWorker',
      label: '随迁子女',
      group: 'other',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'lodgeType',
      label: '住校',
      group: 'other',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'status',
      label: '有效状态',
      group: 'other',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'schoolStatus',
      label: '在校状态',
      group: 'other',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.EDU_STUDENT_STATUS, 'number'),
        placeholder: '请选择在校状态',
      },
    },
    {
      fieldName: 'householdDetail',
      label: '居住地址',
      group: 'other',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入居住地址' },
    },
    {
      fieldName: 'addressDetail',
      label: '户籍所在地详细信息',
      group: 'other',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入户籍所在地详细信息',
      },
    },
    {
      fieldName: 'expertise',
      label: '专长',
      group: 'other',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入专长' },
    },
    {
      fieldName: 'hobby',
      label: '爱好',
      group: 'other',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入爱好' },
    },
    {
      fieldName: 'backInformation',
      label: '备注',
      group: 'other',
      component: 'Textarea',
      formItemClass: 'col-span-3',
      componentProps: { allowClear: true, placeholder: '请输入备注' },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '学号',
      field: 'studentsCode',
    },
    {
      label: '学生姓名',
      field: 'studentName',
    },
    {
      label: '曾用名',
      field: 'beforeName',
    },
    {
      label: '证件类型',
      field: 'cardType',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.SYSTEM_CARD_TYPE,
          value: val,
        }),
    },
    {
      label: '身份证',
      field: 'idCard',
    },
    {
      label: '手机',
      field: 'cellphone',
    },
    {
      label: '性别',
      field: 'sex',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.SYSTEM_USER_SEX,
          value: val,
        }),
    },
    {
      label: '出生日期',
      field: 'birthday',
      render: (val) => formatDate(val) as string,
    },
    {
      label: '学期',
      field: 'semesterName',
    },
    {
      label: '学年',
      field: 'schoolYearName',
    },
    {
      label: '入学年份',
      field: 'enterYear',
    },
    {
      label: '学段',
      field: 'period',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.SYSTEM_PERIOD,
          value: val,
        }),
    },
    {
      label: '学校',
      field: 'schoolIdName',
    },
    {
      label: '年级',
      field: 'gradeIdName',
    },
    {
      label: '班级',
      field: 'classIdName',
    },
    {
      label: '学生专业',
      field: 'studentProfessionId',
    },
    {
      label: '学生类别',
      field: 'studentType',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.EDU_STUDENT_TYPE,
          value: val,
        }),
    },
    {
      label: '学生照片',
      field: 'photoUrl',
      render: (val) => {
        return val
          ? h('img', {
              src: val,
              style:
                'width: 60px; height: 60px; object-fit: contain; border-radius: 6px;',
            })
          : '未上传';
      },
    },
    {
      label: '健康状况',
      field: 'healthStatus',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.SYSTEM_HEALTH_CONDITIONS,
          value: val,
        }),
    },
    {
      label: '是否独生子女',
      field: 'onlyChild',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_BOOLEAN_STRING,
          value: val,
        }),
    },
    {
      label: '是否孤儿',
      field: 'isOrphan',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_BOOLEAN_STRING,
          value: val,
        }),
    },
    {
      label: '是否烈士或优抚子女',
      field: 'isSpecialcare',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_BOOLEAN_STRING,
          value: val,
        }),
    },
    {
      label: '是否留守儿童',
      field: 'isLeftbehind',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_BOOLEAN_STRING,
          value: val,
        }),
    },
    {
      label: '是否随迁子女',
      field: 'isWorker',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_BOOLEAN_STRING,
          value: val,
        }),
    },
    {
      label: '是否住校',
      field: 'lodgeTypeName',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.INFRA_BOOLEAN_STRING,
          value: val,
        }),
    },
    {
      label: '电子邮箱',
      field: 'email',
    },
    {
      label: '微信',
      field: 'weixin',
    },
    {
      label: '民族',
      field: 'nation',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.SYSTEM_NATION,
          value: val,
        }),
    },
    {
      label: '宗教信仰',
      field: 'faith',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.SYSTEM_FAITH,
          value: val,
        }),
    },
    {
      label: '户口性质',
      field: 'residenceType',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.SYSTEM_RESIDENCETYPE,
          value: val,
        }),
    },
    {
      label: '政治面貌',
      field: 'politicalInfo',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.SYSTEM_POLITICALINFO,
          value: val,
        }),
    },
    {
      label: '居住地址',
      field: 'householdDetail',
    },
    {
      label: '户籍所在地详细信息',
      field: 'addressDetail',
    },
    {
      label: '专长',
      field: 'expertise',
    },
    {
      label: '爱好',
      field: 'hobby',
    },
    {
      label: '有效状态',
      field: 'status',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        }),
    },
    {
      label: '在校状态',
      field: 'schoolStatus',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.EDU_STUDENT_STATUS,
          value: val,
        }),
    },
    {
      label: '备注',
      field: 'backInformation',
      render: (val) => h('div', { innerHTML: val }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'studentsCode',
      label: '学号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入学号',
      },
    },
    {
      fieldName: 'studentName',
      label: '学生姓名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入学生姓名',
      },
    },
    {
      fieldName: 'idCard',
      label: '身份证',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入身份证',
      },
    },
    {
      fieldName: 'cellphone',
      label: '手机',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入手机',
      },
    },
    {
      fieldName: 'schoolId',
      label: '学校',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        options: [],
        placeholder: '请选择学校',
      },
    },
    {
      fieldName: 'gradeId',
      label: '年级',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        options: [],
        placeholder: '请选择年级',
      },
    },
    {
      fieldName: 'classId',
      label: '班级',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        options: [],
        placeholder: '请选择班级',
      },
    },
    {
      fieldName: 'schoolStatus',
      label: '在校状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.EDU_STUDENT_STATUS, 'number'),
        placeholder: '请选择在校状态',
      },
    },
    {
      fieldName: 'status',
      label: '有效状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择学生有效状态',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<StudentsApi.Students>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'studentsCode',
      title: '学号',
      minWidth: 120,
    },
    {
      field: 'studentName',
      title: '学生姓名',
      minWidth: 120,
    },
    {
      field: 'idCard',
      title: '身份证',
      minWidth: 120,
    },
    {
      field: 'cellphone',
      title: '手机',
      minWidth: 120,
    },
    {
      field: 'sex',
      title: '性别',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_USER_SEX },
      },
    },
    {
      field: 'schoolId',
      title: '学校',
      minWidth: 120,
    },
    {
      field: 'grade',
      title: '年级',
      minWidth: 120,
    },
    {
      field: 'className',
      title: '班级',
      minWidth: 120,
    },
    {
      field: 'schoolStatus',
      title: '在校状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.EDU_STUDENT_STATUS },
      },
    },
    {
      field: 'status',
      title: '有效状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
